/**
 * @file    stm32h7xx_it.c
 * @brief   Interrupt Service Routines - FreeRTOS + USB CDC
 */

#include "stm32h7xx_hal.h"
#include "FreeRTOS.h"
#include "task.h"

/* External handles */
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
extern TIM_HandleTypeDef htim6;

/**
 * @brief This function handles Non maskable interrupt
 */
void NMI_Handler(void)
{
    while (1)
    {
    }
}

/**
 * @brief This function handles Hard fault interrupt
 */
void HardFault_Handler(void)
{
    while (1)
    {
    }
}

/**
 * @brief This function handles Memory management fault
 */
void MemManage_Handler(void)
{
    while (1)
    {
    }
}

/**
 * @brief This function handles Pre-fetch fault, memory access fault
 */
void BusFault_Handler(void)
{
    while (1)
    {
    }
}

/**
 * @brief This function handles Undefined instruction or illegal state
 */
void UsageFault_Handler(void)
{
    while (1)
    {
    }
}

/**
 * @brief This function handles Debug monitor
 */
void DebugMon_Handler(void)
{
}

/* Note: SVC_Handler, PendSV_Handler, and SysTick_Handler are defined in port.c
 * and renamed via FreeRTOSConfig.h macros to avoid conflicts */

/**
 * @brief This function handles TIM6 global interrupt and DAC1/DAC2 underrun error interrupts
 *
 * TIM6 is used as HAL timebase (1ms tick) to provide HAL_GetTick() and HAL_Delay().
 * This allows FreeRTOS to use SysTick exclusively.
 */
void TIM6_DAC_IRQHandler(void)
{
    HAL_TIM_IRQHandler(&htim6);
}

/**
 * @brief This function handles USB OTG FS global interrupt
 */
void OTG_FS_IRQHandler(void)
{
    HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);
}

/**
 * @brief This function handles EXTI line[15:10] interrupts
 *
 * 处理BMI088加速度计（PC14）和陀螺仪（PC15）的数据就绪中断
 */
void EXTI15_10_IRQHandler(void)
{
    /* BMI088加速度计数据就绪中断 - PC14 */
    if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_14) != RESET)
    {
        __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_14);
        /* 调用HAL GPIO中断回调 */
        HAL_GPIO_EXTI_Callback(GPIO_PIN_14);
    }

    /* BMI088陀螺仪数据就绪中断 - PC15 */
    if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_15) != RESET)
    {
        __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_15);
        /* 调用HAL GPIO中断回调 */
        HAL_GPIO_EXTI_Callback(GPIO_PIN_15);
    }
}

/**
 * @brief This function handles EXTI line[9:5] interrupts
 *
 * 处理BMI270的数据就绪中断（PB7）
 */
void EXTI9_5_IRQHandler(void)
{
    /* BMI270数据就绪中断 - PB7 */
    if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_7) != RESET)
    {
        __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_7);
        /* 调用HAL GPIO中断回调 */
        HAL_GPIO_EXTI_Callback(GPIO_PIN_7);
    }
}
